import { message } from 'antd';
import { history } from 'umi'
import { removeFormStorage } from '@/utils/formHelper';

export function formatResultByFetchDetail(res) {
    if (res) {
        const { code, msg } = res;

        if (code === 10000) {
            return res.data;
        }
        // 配置已变更, 当前访问的数据是旧数据, 要返回列表并刷新列表
        if (code === 21008) {
            message.warning(msg);
            history.push({
                pathname: '/office/personnelManagement/onboardAndLeave/setting/index',
                state: { tabRoute: true, tabRefresh: true, closeTab: true }
            });
            return undefined;
        }
        message.error(msg);
    }
    return undefined;
}

export function onSuccessByDeleteFn(res, onChange) {
    if (res) {
        const { code, msg } = res;

        if (code === 10000) {
            message.success(msg);
            onChange();
        } else if (code === 21008) {
            // 配置已变更, 当前访问的数据是旧数据, 要返回列表并刷新列表
            message.warning(msg);
            history.push({
                pathname: '/office/personnelManagement/onboardAndLeave/setting/index',
                state: { tabRoute: true, tabRefresh: true, closeTab: true }
            });
        } else {
            message.error(msg);
        }
    }
}

export function onSuccessBySave(res, options = {}, activeKey) {
    if (res) {
        const { code, msg } = res;
        const { type, key, action, id } = options;
        if (code === 10000) {
            message.success('提交成功');
            history.push({
                pathname: '/office/personnelManagement/onboardAndLeave/setting/index',
                query: {
                    activeKey
                },
                state: { tabRoute: true, tabRefresh: true, closeTab: true }
            });
            // 如果options.type为rmStorage，则清空对应配置的缓存
            if (type === 'rmStorage') {
                removeFormStorage({ key, action, id });
            }
        } else if (code === 21008) {
            // 配置已变更, 当前访问的数据是旧数据, 要返回列表并刷新列表
            message.warning(msg);
            history.push({
                pathname: '/office/personnelManagement/onboardAndLeave/setting/index',
                query: {
                    activeKey
                },
                state: { tabRoute: true, tabRefresh: true, closeTab: true }
            });
        } else {
            message.error(`提交失败: ${msg}`);
        }
    }
}
